<?

/**
 * excremental
 * ImageHelper - @todo - rename this overengineered shiznit
 * automagic access to asImage methods from objects, implementing iImage interface (returns path)
 *
 *
 *
 * @method string getResizedPath() getResizedPath($image, $width=null, $height=null, $inflate=true, $proportional=false)
 * @method string getResizedPublicPath() getResizedPublicPath($image, $width=null, $height=null, $inflate=true, $proportional=false)
 * @method string getResizedObject() getResizedObject($image, $width=null, $height=null, $inflate=true, $proportional=false)
 *
 * @method string getCroppedPath() getCroppedPath($width=null, $height=null, $left=null, $top=null)
 * @method string getCroppedPublicPath() getCroppedPublicPath($width=null, $height=null, $left=null, $top=null)
 * @method string getCroppedObject() getCroppedObject($width=null, $height=null, $left=null, $top=null)
 *
 * @method string getThumbnailPath() getThumbnailPath($width=null, $height=null, $position="center")
 * @method string getThumbnailPublicPath() getThumbnailPublicPath($width=null, $height=null, $position="center")
 * @method string getThumbnailObject() getThumbnailObject($width=null, $height=null, $position="center")
 *
 * @method string getCenteredCutPath() getCenteredCutPath($width=null, $height=null)
 * @method string getCenteredCutPublicPath() getCenteredCutPublicPath($width=null, $height=null)
 * @method string getCenteredCutObject() getCenteredCutObject($width=null, $height=null)
 *
 * @author asilis
 *
 */
class ImageHelper {
	// Singleton (because php<5.3 doesnt have __callStatic)
    protected static $_instance;
    public static function getInstance(){
		if (!(self::$_instance instanceof self)){
			self::$_instance = new self();
		}
		return self::$_instance;
    }

	/*
	 *
	 * get*PublicPath($image, $param1, $param2, ...
	 * get*Path($image, $param1, $param2, ...
	 *
	 */
	public function __call($methodName, $arguments){
		if (strpos($methodName, "get")!==0){
			//return false;
			throw new NoSuchMethodException;
		}

		$asImageMethodName=substr($methodName, 3);
		$returnPublicPath=null;
		if (strrpos($methodName, "PublicPath")==(strlen($methodName)-10)){
			$asImageMethodName=substr($asImageMethodName, 0, -10);
			$returnType="PublicPath";
		}else if (strrpos($methodName, "Path")==(strlen($methodName)-4)){
			$asImageMethodName=substr($asImageMethodName, 0, -4);
			$returnType="Path";
		}else if (strrpos($methodName, "Object")==(strlen($methodName)-6)){
			$asImageMethodName=substr($asImageMethodName, 0, -6);
			$returnType="Object";
		}else{
			//return false;
			throw new NoSuchMethodException;
		}

		$asImageMethodName=trim($asImageMethodName);
		if (empty($asImageMethodName)){
			//return false;
			throw new NoSuchMethodException;
		}

		$asImageMethodName="get{$asImageMethodName}";

		if (!method_exists("asImage", $asImageMethodName)){
			//return false;
			throw new NoSuchMethodException;
		}

		$image=$arguments[0];

		if (!empty($image) && !($image instanceOf iImage) && ($image instanceOf sfOutputEscaperObjectDecorator || $image instanceOf sfOutputEscaperIteratorDecorator)){
			$image=$image->getRawValue();
		}

		if (empty($image) || !($image instanceOf iImage)){
			return false;
		}

		unset($arguments[0]);

		$imagePath=$image->getFullPath();

		$asImage=new asImage($imagePath);

		if ($asImage===false){ // file doesnt exists
			return false;
		}

		$res=null;
		if ($asImage instanceOf asImage){
			$res=call_user_func_array(array($asImage, $asImageMethodName), $arguments);
		}

		if (!empty($res) && $res instanceOf asImage){
			switch($returnType){
				case "PublicPath":
					return $res->getPublicPath();
					break;
				case "Path":
					return $res->getFilename();
					break;
				case "Object":
				default:
					return $res;
					break;
			}
		}

	}

}

class NoSuchMethodException extends Exception {

}





